package hn.cch.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import hn.cch.entity.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class JsonUtil {

    public static Logger logger = LoggerFactory.getLogger(JsonUtil.class);


    public static <T> T toJsonObject(String string, Class<T> clazz){
        ObjectMapper objectMapper = new ObjectMapper();
        T object = null;
        try {
            object = objectMapper.readValue(string, clazz);
        } catch (IOException e) {
            // e.printStackTrace();
            logger.error("toJsonObject:{}", e.getMessage());
        }
        return object;
    }


    public static String toJsonString(Object object){
        ObjectMapper objectMapper = new ObjectMapper();
        String string = null;
        try {
            string = objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            // e.printStackTrace();
            logger.error("toJsonString:{}", e.getMessage());
        }
        return string;
    }
}
